/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.tags;

import ca.trader.logger.TraderLogger;
import ca.trader.sale.Sale;
import ca.trader.utils.HttpConsts;
import ca.trader.utils.TraderStringUtils;
import java.util.Date;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.ocpsoft.prettytime.PrettyTime;

/**
 *
 * @author Ahsan
 */
public class PrintSaleListTable extends SimpleTagSupport
{

	 private static final String _className = PrintSaleListTable.class.getName();
	 private List<Sale> saleList;
	 private static final int MAX_TITLE_LENGTH_TO_PRINT = 150;
	 private static final int MAX_DESC_LENGTH_TO_PRINT = 500;
	 
	 private static final String SALE_DESC_TD_CLASS = "saledesc";
	 private static final String SALE_PRICE_TD_CLASS = "saleprice";
	 private static final String SALE_LOCATION_TD_CLASS = "salelocation";
	 private static final String SALE_CREATED_BY_TD_CLASS = "salecreated";
	 private static final String SALE_CATEGORY_TD_CLASS = "salecategory";

	 public List<Sale> getSaleList()
	 {
		  return saleList;
	 }

	 public void setSaleList(List<Sale> saleList)
	 {
		  this.saleList = saleList;
	 }

	 /**
	  * Called by the container to invoke this tag. The implementation of this
	  * method is provided by the tag library developer, and handles all tag
	  * processing, body iteration, etc.
	  */
	 @Override
	 public void doTag() throws JspException
	 {
		  final String _methodName = "doTag";
		  JspWriter out = getJspContext().getOut();
		  PrettyTime currentTime = new PrettyTime(new Date());

		  try
		  {
			   out.println("<table border=\"1\" class=\"saleListTable\">");
			   out.println(getTableHeaderRow());
			   
			   
			   String rowTemplate = getTableRowTemplate ();
			   
			   
			   for (Sale sale : saleList)
			   {
				    out.println(String.format(rowTemplate, sale.getId(), TraderStringUtils.trimStringToLength(sale.getTitle(), MAX_TITLE_LENGTH_TO_PRINT), 
						  TraderStringUtils.trimStringToLength(sale.getDescription(), MAX_DESC_LENGTH_TO_PRINT),
						  sale.getPrice(), sale.getLocation(), currentTime.format(sale.getCreationDate()), sale.getCategory()));
			   }
			   out.println("</table>");

		  }
		  catch (java.io.IOException ex)
		  {
			   TraderLogger.logSevere(_className, _methodName, "Unknown exception", ex);
		  }
	 }

	 private String getTableHeaderRow()
	 {
		  return String.format("<tr><th class=\"%s\">Description</th><th class=\"%s\">Price"
				+ "</th><th class=\"%s\">Location</th><th class=\"%s\">Created"
				+ "</th><th class=\"%s\">Category</th></tr>", SALE_DESC_TD_CLASS, SALE_PRICE_TD_CLASS, SALE_LOCATION_TD_CLASS,
				SALE_CREATED_BY_TD_CLASS, SALE_CATEGORY_TD_CLASS);
	 }
	 
	 private String getTableRowTemplate ()
	 {
		  return String.format("<tr><td class=\"%s\"><b><a href=\"%s?id=%%d\"> %%s"
				+ "</b><br/>%%s</a></td><td class=\"%s\">%%s</td><td class=\"%s\">%%s</td><td class=\"%s\">%%s"
				+ "</td><td class=\"%s\">%%s</td></tr>", SALE_DESC_TD_CLASS, HttpConsts.VIEW_SALE_JSP_PAGE, SALE_PRICE_TD_CLASS, 
				SALE_LOCATION_TD_CLASS, SALE_CREATED_BY_TD_CLASS, SALE_CATEGORY_TD_CLASS);
	 }

	 
}
